package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.system.provider.bean.Attitude;
import cn.sanshuilin.kst.system.provider.core.CurrentInfo;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-11-29 21:27
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class AttitudeReq extends AbstractJpaDao<Attitude> {

    public List<Attitude> getAttitudesByPubMemberID(int nPageIndex, int nPageSize, String pubMemberID) {
        String hql = "FROM Attitude a WHERE  a.PubMemberID= :PubMemberID ORDER BY a.PubDate";
        Query query = entityManager.createNativeQuery(hql, Attitude.class);
        query.setParameter("PubMemberID", CurrentInfo.getCurrentMember().getId());
        query.setFirstResult(nPageIndex * nPageSize);
        query.setMaxResults(nPageSize);
        List<Attitude> list = query.getResultList();
        return list;
    }


    public List<Attitude> getAttitudesByObjID(int nPageIndex, int nPageSize, String strObjID) {
        String hql = "FROM Attitude a WHERE a.ObjID= :ObjID ORDER BY a.PubDate";
        Query query = entityManager.createNativeQuery(hql, Attitude.class);
        query.setParameter("ObjID", strObjID);
        query.setFirstResult(nPageIndex * nPageSize);
        query.setMaxResults(nPageSize);
        List<Attitude> list = query.getResultList();
        return list;
    }


    public Object updateCount(String ID) {
        Query query = entityManager.createNativeQuery("SELECT Name FROM TableIndex  WHERE ID = :ID");
        query.setParameter("ID", ID);
        return query.getSingleResult();
    }


    public Attitude getAttitudeByObjIDAndPubMemberID(String objID, String PubMemberID) {

        String hql = "FROM Attitude a WHERE a.PubMemberID = :PubMemberID AND  a.ObjID = :ObjID  ";
        Query query = entityManager.createNativeQuery(hql, Attitude.class);
        query.setParameter("ObjID", objID);
        query.setParameter("PubMemberID", PubMemberID);
        return (Attitude) query.getSingleResult();
    }

}
